from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

# 获取Edge浏览器驱动
driver = webdriver.Edge()
# 打开指定网址
driver.get('https://www.jiuxian.com/')
# 强等2秒
sleep(1)
# 窗口最大化
driver.maximize_window()
sleep(1)
# 点击请登录
driver.find_element(By.CLASS_NAME,'headerLogin').click()
sleep(1)
# 输入账号
driver.find_element(By.ID,"userName").send_keys('18185603008')
sleep(2)
# 输入密码
driver.find_element(By.ID,'password').send_keys('123456Aa')
sleep(10)
#点击登录
driver.find_element(By.NAME,'bi_Usuallogin').click()
sleep(2)
# 点击确定
driver.find_element(By.ID,'bindSub').click()
sleep(2)
# 断言
dy1 = driver.find_element(By.XPATH,'/html/body/div[2]/div/div[1]/div/p/span').text
assert dy1 == 'jxw824726828'
print(dy1)
sleep(2)
# 向下滚动1500像素
driver.execute_script("window.scrollTo(0,500)")
sleep(2)
# 精准匹配文本为“疯狂抢购”的元素
# driver.find_element(By.XPATH,'//*[text()=\'疯狂抢购\']').click()
# sleep(2)
# 模糊匹配包含"疯狂抢购"的元素(更宽松)
driver.find_element(By.XPATH,'//*[contains(text(),"疯狂抢购")]')
sleep(2)
# 点击 西班牙小红帽酒
driver.find_element(By.XPATH,'//*[@id="crazySale"]/div[1]/div[2]/div[1]/ul/li[5]/div[1]/a/img').click()
sleep(2)
# 窗口切换
driver.switch_to.window(driver.window_handles[-1])
sleep(1)
# 断言2
dy2 = driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/h1').text
print(dy2)
# assert '西班牙奥兰小红帽半甜红葡萄酒750ml*2+田园风双支礼袋+酒刀' in dy2
sleep(2)
# 断言3
dy3 = driver.find_element(By.XPATH,'//*[@id="nowPrice"]/span[1]/strong').text
print(dy3)
assert '88.00' == dy3
sleep(2)
# 点击加入购物车
driver.find_element(By.ID,'addToCartForDetail').click()
sleep(2)
# 断言4
dy4 = driver.find_element(By.XPATH,'//*[@id="u-buy-layId"]/div/div[2]/span').text
print(dy4)
assert '已成功加入购物车' == dy4
sleep(2)
# 点击继续购物
driver.find_element(By.CLASS_NAME,'u-buy-g').click()
sleep(2)
# 点击收藏
driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[3]/div[2]/div[1]/a/i').click()
sleep(2)
# alert弹窗
alert = driver.switch_to.alert
alert.accept()
sleep(2)
# 点击收藏列表
driver.find_element(By.XPATH,'/html/body/div[16]/div/div[1]/div[2]').click()
sleep(2)
# 点击收藏列表
driver.find_element(By.XPATH,'/html/body/div[16]/div/div[1]/div[2]').click()
sleep(2)
# 窗口切换
driver.switch_to.window(driver.window_handles[-1])
sleep(2)
# 断言5 收藏的酒是否和收藏列表中的酒一样
dy5 = driver.find_element(By.XPATH,'/html/body/div[4]/div[3]/div[2]/table/tbody/tr[2]/td[2]/div[2]/a').text
print(dy5)
assert dy2 == dy5
sleep(2)
# 断言6 收藏的酒是否和收藏列表中的价格一致
dy6 = driver.find_element(By.CLASS_NAME,'mycT4Txt').text
print(dy6)
assert dy3 in dy6
# 点击取消收藏
driver.find_element(By.CLASS_NAME,'mycol-can').click()
sleep(2)
# 断言7
dy7 = driver.find_element(By.XPATH,'/html/body/div[8]/div/div[2]/p/span').text
print(dy7)
assert '取消收藏失败！' not in dy7
sleep(2)
# 点击确定
driver.find_element(By.XPATH,'/html/body/div[8]/div/div[3]/span[1]/b').click()
sleep(2)
# 断言8
dy8 = driver.find_element(By.XPATH,'/html/body/div[8]/div/div[2]/p/span').text
print(dy8)
assert dy8 == '取消收藏成功!'
sleep(5)